//
//  BC_Move.m
//  GameTree
//
//  Created by BrunoCausse on 13/11/10.
//  Copyright 2010-2013 poulailler cie. All rights reserved.
//

#import "BC_Move.h"

NSString * const BCTitleKey		= @"title";
NSString * const BCScoreKey		= @"score";
NSString * const BCTimeKey		= @"time";
NSString * const BCCommentKey	= @"comment";
NSString * const BCImageIDKey	= @"imageID";
NSString * const BCPVKey		= @"pv";


@implementation BC_Move

#pragma mark init & dealloc methods

- (id)init
{
	return [self initWithLocation:NOMOVE];
}

- (id)initWithLocation:(int)pos
{
	self = [super init];
	if (self) {
		if(pos == NOMOVE) {
			self.title = @"??";
		} else if (pos == PASS) {
			self.title = @"Ps";
		} else {
			self.title = [BC_Move indexToString:pos];
		}
		
		position[0] = pos;
		score = UNDEFSCORE;
		time = UNDEFTIME;
		comment = [[NSAttributedString alloc] initWithString:@""];
		imageID = -1;
	}
	
	return self;
}

- (void)dealloc
{
	[title release];
	[comment release];
	[super dealloc];
}


#pragma mark getter setter methods

@synthesize title;
@synthesize comment;
@synthesize nFlips, imageID;
@synthesize score, time;
@synthesize pv;
@synthesize hashcode;


- (int) positionAtIndex:(int)index
{
	return position[index];
}

- (void) setPosition:(int)value atIndex:(int)index
{
	position[index] = value;
}


#pragma mark convenience methods

+ (NSString *)indexToString:(int)aIndex
{
    if (aIndex == NOMOVE) {
        return @"--";
    }
	if (aIndex == PASS) {
		return @"Ps";
	}
	
	NSRange r = NSMakeRange((aIndex%9 -1), 1);
	return[[@"ABCDEFGH" substringWithRange:r] stringByAppendingFormat:@"%d", (aIndex/9)];
	
}

//+ (const NSString *)indexToString:(int)aIndex
//{
// 	
//	const NSString *absice[] = 
//    {   @"--", @"Ps", @"--", @"--", @"--", @"--", @"--", @"--", @"--", @"--",
//        @"A1", @"B1", @"C1", @"D1", @"E1", @"F1", @"G1", @"H1", @"--",
//        @"A2", @"B2", @"C2", @"D2", @"E2", @"F2", @"G2", @"H2", @"--",
//        @"A3", @"B3", @"C3", @"D3", @"E3", @"F3", @"G3", @"H3", @"--",
//        @"A4", @"B4", @"C4", @"D4", @"E4", @"F4", @"G4", @"H4", @"--",
//        @"A5", @"B5", @"C5", @"D5", @"E5", @"F5", @"G5", @"H5", @"--",
//        @"A6", @"B6", @"C6", @"D6", @"E6", @"F6", @"G6", @"H6", @"--",
//        @"A7", @"B7", @"C7", @"D7", @"E7", @"F7", @"G7", @"H7", @"--",
//        @"A8", @"B8", @"C8", @"D8", @"E8", @"F8", @"G8", @"H8", @"--",
//
//    };
//	
//	return absice[aIndex];		//autorelease
//}

	
+ (int)stringToIndex:(NSString *)aString
{

	NSString * coord = [aString uppercaseString];
    
    if([coord isEqual:@"--"])
        return NOMOVE;

	if([coord isEqual:@"PS"] || [coord isEqual:@"PA"])
		return PASS;

	int index = NOMOVE;
	
	switch ([coord characterAtIndex:0]) {
		case 'A':
			index = 1;
			break;
		case 'B':
			index = 2;
			break;
		case 'C':
			index = 3;
			break;
		case 'D':
			index = 4;
			break;
		case 'E':
			index = 5;
			break;
		case 'F':
			index = 6;
			break;
		case 'G':
			index = 7;
			break;
		case 'H':
			index = 8;
			break;
	}

	switch ([coord characterAtIndex:1]) {
		case '1':
			index += 9;
			break;
		case '2':
			index += 18;
			break;
		case '3':
			index += 27;
			break;
		case '4':
			index += 36;
			break;
		case '5':
			index += 45;
			break;
		case '6':
			index += 54;
			break;
		case '7':
			index += 63;
			break;
		case '8':
			index += 72;
			break;
	}
	
	return index;
	
}

- (void)XORHashcode:(unsigned long long)hash
{
    hashcode ^= hash;
}

#pragma mark un/archiving methods

+ (BC_Move*)moveWithContents:(NSDictionary *)dictionary
{
	BC_Move *move = [[BC_Move alloc] init];
	
	move.title		= [dictionary valueForKey:BCTitleKey];
	
	
	[move setPosition:[BC_Move stringToIndex:move.title] atIndex:0];
	
	
	NSNumber *score	= ([dictionary valueForKey:BCScoreKey] != nil)?
						[dictionary valueForKey:BCScoreKey]:
						[NSNumber numberWithFloat:UNDEFSCORE];
	[move setValue:score forKey:BCScoreKey];
	
	NSNumber *time	= ([dictionary valueForKey:BCTimeKey] != nil)?
						[dictionary valueForKey:BCTimeKey]:
						[NSNumber numberWithFloat:UNDEFTIME];
	[move setValue:time forKey:BCTimeKey];
	
	if([dictionary valueForKey:BCCommentKey]) {
		//decode the NSData as attributedString
		NSData *attributedStringData = [dictionary valueForKey:BCCommentKey];
		move.comment = [NSKeyedUnarchiver unarchiveObjectWithData:attributedStringData];
	}
	
	NSNumber *ID		= ([dictionary valueForKey:BCImageIDKey] != nil)?
						[dictionary valueForKey:BCImageIDKey]:
						[NSNumber numberWithInt:-1];
	
	[move setValue:ID forKey:BCImageIDKey];
	
	return [move autorelease];
	
	
	
}



- (NSDictionary *)contents
{
	NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
	
	[dict setValue:title forKey:BCTitleKey];
	if(score != UNDEFSCORE)
		[dict setValue:[NSNumber numberWithFloat:score]	forKey:BCScoreKey];
	if(time != UNDEFTIME)
		[dict setValue:[NSNumber numberWithFloat:time]	forKey:BCTimeKey];

	//encode the attributedString as NSData
	NSData* attributedStringData = [NSKeyedArchiver archivedDataWithRootObject:comment];
	[dict setValue:attributedStringData forKey:BCCommentKey];

	if(imageID >= 0)
		[dict setValue:[NSNumber numberWithInt:imageID]	forKey:BCImageIDKey];
	
	return dict;	//autorelease
}
		
#pragma mark utilities methods
	
- (int)increment_nFlips
{
	return ++nFlips;
}

- (NSString *)description
{
	
	int pos = position[0];
	
	if(pos == NOMOVE) {
		return @"  ";
	} else if(pos == PASS ||(pos>=A1 && pos<=H8 && pos%9>0)) {
		return [BC_Move indexToString:pos];
	}
	
	return @"??";
}
	
							 
	



@end
